<?php
/*
*All rights reserved: Json.
*Links:http://www.liyaoquan.cn.
*Links:http://imarkchina.cn.
*Links:http://www.imarkchina.cn.
*/

/**
 * 博客后台评论模型，负责数据，增，删，改，查
 * 因为没有博客数据没使用数据库，所以没有继承 Model 类
 */
Class GetpinglunModel extends PublicModel{

	/**
	 * 处理评论首页显示数据
	 */
	Public function Show(){
		$pinglun = $this->getPingLun();
		$pageurl = U('index', '', true);
		$indexpage = 12;
		$count = count($pinglun);
		$page = new Page($count, $indexpage, $pageurl);
		$pnum = explode(',', $page->limit());
		$data = array();
		$i = -1;
		foreach ($pinglun as $k => $v) {
			$i++;
			if ($i < $pnum[0] || $i >= $pnum[0] + $pnum[1]) continue;
			$data[] = unfile(C('pinglun.data').$k.'.Mark');
		}
		foreach ($data as $k => $v) {
			$data[$k]['content'] = $this->msgReplace($v['content']);
			$data[$k]['reusername'] = PinglunLeve::getUsername($data, $v['pid']);
			$posttitle = unfile(C('post.postdata').$v['id'].'.Mark');
			$data[$k]['posttitle'] = $posttitle['title'];
			if (!empty($v['user_id'])) {
				$user = loadfile(C('user').$v['user_id'].'.php');
				if ($user) {
					$data[$k]['username'] = $user['username'];
					$data[$k]['usermsg'] = $user['msg'];
					$data[$k]['url'] = $user['url'];
					$data[$k]['useremail'] = $user['email'];
				}
			}
			if ($v['username'] == '@i@Mark@China@') {
				$config = $this->blogConfig();
				$data[$k]['username'] = $config['user_nick'];
			}
		}
		$redata['pinglun'] = $data;
		$redata['page'] = $page->view();
		return $redata;
	}

	/**
	 * 处理评论删除数据
	 * @param  boolean $fileid [description]
	 * @param  boolean $id     [description]
	 * @return [type]          [description]
	 */
	Public function deletePingLun($fileid = false, $id = false){
		if ($fileid && $id) {
			$file = loadfile(C('pinglun.id').$id.'.php');
			unset($file[$fileid]);
			uasort($file, 'Post_Sort');
			if (varfile(C('pinglun.id').$id.'.php', $file) && unlink(C('pinglun.data').$fileid.'.Mark')) {
				return true;
			}
		}
	}

	/**
	 * 保存回复评论数据处理
	 * @param  [type] $array [description]
	 * @return [type]        [description]
	 */
	Public function savePunLun($array){
		$array['username'] = '@i@Mark@China@';
		$file = C('pinglun.id').$array['id'].'.php';
		$fid = loadfile($file);
		$dir = C('pinglun.data');
		$filename = randCode(6, 0);
		$day = date('Y.m.d');
		$time = date('H:i:s', time());
		if (file_exists($dir.$filename.'.Mark')) {
			$filename = randCode(6, 0);
		}
		$array['rid'] = (count(GetFileList($dir)) + 1);
		$array['date'] = $day;
		$array['time'] = $time;
		$array['fileid'] = $filename;
		$put = array(
			'date' => $day,
			'time' => $time,
			);
		$fid[$filename] = $put;
		uasort($fid, 'Post_Sort');
		if (varfile($file, $fid) && serfile($dir.$filename.'.Mark', $array)) {
			return true;
		}
	}
}